import os
import sys
import cmd

import traceback


'''
quit
exit
'''

class shell(cmd.Cmd):
    def __init__(self, prompt="/>", completekey='tab', stdin=None, stdout=None, log=None):
        super().__init__(completekey, stdin, stdout)
        self.prompt = prompt
        if log == None:
            from logger import log
        self.log=log
    def emptyline(self):
        pass
    def precmd(self, line):
        if len(line) > 0:
            # log.info("shell%s"%(line.split()))
            self.log.info("shell->(%s)"%(line))
        return super().precmd(line)
    def default(self, line):
        self.log.warning('*** Unknown syntax: %s'%line)
    def onecmd(self, line):
        try:
            result = super().onecmd(line)
        except Exception as e:
            self.log.error("Input-Exception(%s)\n%s"%(str(e), traceback.format_exc()))
            result = False
        return result

    def do_test(self, arg):
        self.log.debug("%s %s %s"%(self, type(arg), arg))
    def do_quit(self, arg):
        return True
    def do_shell(self, arg):
        os.system(arg)


if __name__ == '__main__':
    sh = shell()
    sh.cmdloop()
    log.info("退出")